<?php

namespace app\api\controller;

use queue\Queue;
use think\Log;
use tool\Res;

class Index extends Base
{


    public function slide()
    {
        $list = model("slide")->order("sort asc,id asc")->select();
        return s($list);
    }

    public function upload()
    {
        $this->getuser();
        $file = Res::upload(i("dir"));
        return s($file);
    }

    public function lists()
    {
        $lists["cate"] = model("cate")->field("name,pic,id")->where("isshow_sy", 1)->where("isshow", 1)->order("sort asc")->select();
        $lists['goods'] = model("goods")->field("name,price,id,sales,number,pic")->where("delete_time", "null")->where("isshow_sy", 1)->order("sort asc")->select();
        $lists['hot_goods'] = model("goods")->field("name,price,id,sales,number,pic")->where("delete_time", "null")->order("sales desc")->limit(16)->select();
        return s($lists);
    }

    public function tocate()
    {
        $id = input("id");
        $cate = model("cate")->where("id", $id)->find();
        if (empty($cate)) {
            e("分类不存在");
        }
        $goods = model("goods")->where("cate_id", $id)->where("isshow", 1)->where("isdelete", 0)->order("sort asc")->select();
        return s($goods);
    }

    public function getjssdk()
    {
        $app = $this->getwxapp();
        $url = input("url");
        $app->jssdk->setUrl($url);
        $apilist = input("apilist");
        $result = $app->jssdk->buildConfig(explode("|", $apilist), false, false, false);
        return s($result);
    }


    public function wx_init()
    {
        $app = $this->getwxapp();

        $app->server->push(function ($message) use ($app) {
            $default = "您好！欢迎关注微拼优品拼团!";
            if ($message['MsgType'] != "event") {
                return $default;
            }
            if ($message['Event'] != "SCAN" && $message['Event'] != "subscribe") {
                return $default;
            }
            $openid = $message['FromUserName'];
            $user = model("user")->where("openid", $openid)->find();
            $isnewuser = false;
            if (empty($user)) {
                model("user")->insert(['openid' => $openid, "reg_time" => now()]);
                $user = model("user")->where("openid", $openid)->find();
                $isnewuser = true;
            }
            $user_id = $user['id'];
            $wx_user = $app->user->get($openid);
            if (!empty($wx_user) && !empty($wx_user['nickname'])) {
                model("user")->where("id", $user_id)->update([
                    "nickname" => $wx_user['nickname'],
                    "sex" => $wx_user['sex'],
                    "city" => $wx_user['city'],
                    "province" => $wx_user['province'],
                    "country" => $wx_user['country'],
                    "avatar_before" => str_replace("/132", "/0", $wx_user['headimgurl']),
                ]);
                doRequest("https://server.xckj789.com/api/user/setavatar", ["user_id" => $user_id]);
            }
            $eventkey = $message['EventKey'];
            $code = str_replace("qrscene_", "", $eventkey);
            if (intval($code < 50000)) {
                return $default;
            }
            $code = intval($code) - 50000;
            $group = $this->bindgroup($user_id, $code);
            if ($group === false) {
                return $default;
            }
            if ($isnewuser) {
                integral($code, 100);
            }
            return "您好,欢迎关注微拼优品拼团，推荐您的群主为:{$group['personal_name']},祝您购物愉快!!!";
        });
        $response = $app->server->serve();
        $response->send();
        exit; // Laravel 里请使用：return $response;
    }
}
